"""
绘制K线图
"""
from pyecharts.charts import Kline
from pyecharts.options import AxisOpts, SplitAreaOpts, AreaStyleOpts, DataZoomOpts

from utils import create_connection

conn = create_connection(database='viz')
try:
    with conn.cursor() as cursor:
        cursor.execute(
            'select date(`TxDate`) from tb_stock order by `TxDate`'
        )
        x_data = [str(row[0]) for row in cursor.fetchall()]
        cursor.execute(
            'select round(`Open`, 2), round(`Close`, 2), round(`Low`, 2), round(`High`, 2) '
            'from tb_stock order by `TxDate`'
        )
        y_data = cursor.fetchall()
        kline_chart = Kline()
        kline_chart.add_xaxis(x_data)
        kline_chart.add_yaxis('', y_data)
        kline_chart.set_global_opts(
            yaxis_opts=AxisOpts(
                is_scale=True,
                splitarea_opts=SplitAreaOpts(
                    is_show=True, areastyle_opts=AreaStyleOpts(opacity=1)
                )
            ),
            datazoom_opts=[DataZoomOpts(pos_bottom="-2%")]
        )
        kline_chart.render()
finally:
    conn.close()
